package com.foreveross.proxyip.infra.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
 * 日期辅助类
 * @author xiangsf
 * @version $Revision$ $Date$
 * @since 0.5
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	/**
	 * 以下定义了日期的不同格式
	 * 
	 */
	public final static DateFormat DATE_FMT_US_E = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",Locale.US);
	public final static DateFormat DATE_FMT_US_MDY_HMSA = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa",Locale.US);
	public final static DateFormat DATE_FMT_US_MDY_HMA_S = new SimpleDateFormat("MM/dd/yyyy hh:mm aa",Locale.US);
	public final static DateFormat DATE_FMT_US_MDY_HMA = new SimpleDateFormat("MM/dd/yyyy hh:mmaa",Locale.US);
	public final static DateFormat DATE_FMT_US_MDYS_HMA_S = new SimpleDateFormat("MM dd yyyy hh:mm aa",Locale.US);
	public final static DateFormat DATE_FMT_US_MDYS_HMA = new SimpleDateFormat("MM dd yyyy hh:mmaa",Locale.US);
	
	public final static DateFormat DATE_FMT_CN_YMD = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_H = new SimpleDateFormat("yyyy-MM-dd HH",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_HM = new SimpleDateFormat("yyyy-MM-dd HH:mm",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_HMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_BARS_YMD_HMSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS",Locale.CHINA);
	
	public final static DateFormat DATE_FMT_CN_SLASH_YMD_H = new SimpleDateFormat("yyyy/MM/dd HH",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_SLASH_YMD_HM = new SimpleDateFormat("yyyy/MM/dd HH:mm",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_SLASH_YMD_HMS = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_SLASH_YMD_HMSS = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS",Locale.CHINA);
	
	public final static DateFormat DATE_FMT_CN_DOT_YM = new SimpleDateFormat("yyyy.MM",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_DOT_YMD = new SimpleDateFormat("yyyy.MM.dd",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_NULL_YMD = new SimpleDateFormat("yyyyMMdd",Locale.CHINA);
	public final static DateFormat DATE_FMT_CN_NULL_Y2MD = new SimpleDateFormat("yyMMdd",Locale.CHINA);
	/**
	 * 获得当前月份的第一天
	 * @return
	 */
	public static Date getCurrentMonthFirstDate(){
		return getMonthFirstDate(new Date());
	}
	/**
	 * 获得指定日期的月份第一天
	 * @return
	 */
	public static Date getMonthFirstDate(Date date){
		if(date == null) return getCurrentMonthFirstDate();
		Date currentDate = DateUtils.setDays(date, 1);
		return currentDate;
	}
	/**
	 * 获得当前月份的最后一天
	 * @return
	 */
	public static Date getCurrentMonthEndDate(){
		return getMonthEndDate(new Date());
	}
	/**
	 * 获得指定日期的月份最后一天
	 * @return
	 */
	public static Date getMonthEndDate(Date date){
		if(date == null) return getCurrentMonthEndDate();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Date currentDate =  DateUtils.setDays(date, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return currentDate;
	}
	
	public static void main(String[] arg){
//		Calendar cal = Calendar.getInstance();
//		cal.set(2013, 2, 5);
//		log.debug(DATE_FMT_CN_YMD.format(cal.getTime()));
//		log.debug(DATE_FMT_CN_YMD.format(getQuarterFirstDate(cal.getTime())));
//		log.debug(DATE_FMT_CN_YMD.format(getQuarterEndDate(cal.getTime())));
	}
	/**
	 * 获得当前季度的第一天
	 * @return
	 */
	public static Date getCurrentQuarterFirstDate(){
		return getQuarterFirstDate(new Date());
	}
	/**
	 * 获得指定日期的季度第一天
	 * @return
	 */
	public static Date getQuarterFirstDate(Date date){
		if(date == null) return getCurrentQuarterFirstDate();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.MONTH, ((cal.get(Calendar.MONTH)/3+1)*3-2)-1);
		Date currentDate =  DateUtils.setDays(cal.getTime(), 1);
		return currentDate;
	}
	/**
	 * 获得当前季度的最后一天
	 * @return
	 */
	public static Date getCurrentQuarterEndDate(){
		return getQuarterEndDate(new Date());
	}
	
	/**
	 * 获得当前季度的最后一天
	 * @return
	 */
	public static Date getQuarterEndDate(Date date){
		if(date == null) return getCurrentQuarterFirstDate();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.MONTH, (cal.get(Calendar.MONTH)/3+1)*3-1);
		Date currentDate =  DateUtils.setDays(cal.getTime(), cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		return currentDate;
	}
}
